package com.esgov.chatbot.api;

import com.esgov.chatbot.dify.DifyClient;
import com.esgov.chatbot.dify.DifyResponse;
import com.esgov.chatbot.support.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Dify智能问答接口控制器
 */
@RestController
@RequestMapping("/api/dify")
@Api(tags = "Dify智能问答接口")
public class DifyController {

    @Autowired
    private DifyClient difyClient;

    /**
     * 工单统计问答接口
     *
     * @param requestBody 请求体
     * @return 问答结果
     */
    @PostMapping("/chat-messages")
    @ApiOperation("智能问答接口")
    public ResponseResult<DifyResponse> chatMessages(@RequestBody Map<String, Object> requestBody) {
        try {
            String query = (String) requestBody.get("query");
            String conversationId = (String) requestBody.get("conversation_id");
            String user = (String) requestBody.get("user");
            
            DifyResponse response = difyClient.sendMessage(
                    query,
                    conversationId != null ? conversationId : "",
                    user != null ? user : "default_user"
            );
            
            return ResponseResult.success(response);
        } catch (Exception e) {
            return ResponseResult.<DifyResponse>fail("问答服务暂时不可用: " + e.getMessage());
        }
    }
}